<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/4
 * Time: 15:59
 */

class mRedis {

    private static $_redis;

    public static function get_instance(){

        $config = array(
            'host'  =>  '127.0.0.1',
            'port'  => 6379,
            'auth'  => ''
        );

        if (extension_loaded('redis')) {
            if (!(self::$_redis instanceof self)) {
                try {
                    self::$_redis = new Redis();
                    self::$_redis->connect($config['host'], $config['port']);
                    if ($config['auth'] != '') {
                        self::$_redis->auth(trim($config['auth']));
                    }
                } catch (Exception $ex) {
                    var_dump($ex->getMessage());
                    exit('redis error');
                    return false;
                }
            }
            return self::$_redis;
        } else {
            exit('redis not loaded');
            return false;
        }
    }

}

class RedisTest {

    public $redis;

    public function __construct()
    {
        $this->redis = mRedis::get_instance();
    }

    public function getRandChar($length){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $max = strlen($strPol)-1;

        for($i=0;$i<$length;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $str;
    }

    public function microtime(){
        $mtime=explode(' ',microtime());
        return $mtime[1]+$mtime[0];
    }

    public function set($key,$value){
        $this->redis->set($key,$value);
    }
}

$test = new RedisTest();

$t1 = $test->microtime();
for($i=0;$i<200;$i++){
    $test->set($i,$test->getRandChar(6));
}
$t2 = $test->microtime();
var_dump($t2-$t1);